<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table
          ref="dataList"
          :data="menuList"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          border row-key="id"
          style="width:100%;margin-bottom: 20px;"
        >
          <el-table-column label="序号" prop="id"/>
          <el-table-column label="图标" prop="icon">
            <template slot-scope="{row}">
              <img :alt="row.type_name" :src="row.icon">
            </template>
          </el-table-column>
          <el-table-column label="分类名称" prop="type_name"/>
          <el-table-column label="等级" prop="level"/>
          <el-table-column label="排序" prop="sort"/>
          <el-table-column label="房间" prop="icon">
            <template slot-scope="{row}">
              <span v-if="row.nature === 0">公会房间</span>
              <span v-else>私人房间</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <!-- <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        ref="editRulesForm"
        :model="editRulesForm"
        :rules="editRulesFormRules"
        class="demo-ruleForm"
        label-width="120px"
      >
        <el-form-item label="分类名称" prop="room_type_name">
          <el-input v-model="editRulesForm.room_type_name" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="editRulesForm.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-input v-model.number="editRulesForm.level" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img
              v-if="editRulesForm.icon"
              :src="editRulesForm.icon"
              alt=""
              class="w-32 h-32"
            />
            <el-button v-else class="el-icon-plus avatar-uploader-icon">上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="官方大类" prop="higher_id">
          <el-select
            v-model="editRulesForm.higher_id"
            filterable
            placeholder="请选择官方大类"
          >
            <el-option
              v-for="item in OfficialTypeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间" prop="nature">
          <el-select
            v-model="editRulesForm.nature"
            filterable
            placeholder="请选择房间"
          >
            <el-option
              v-for="item in roomLevel"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { deleteData, listData, postAddData, putEditData, roomTypeList } from '@/api/roomType'

export default {
  data() {
    return {
      // IMG_UPLOAD_URL,
      confirmBtn: false,
      // 要修改的当前行的id
      row_id: 0,
      // importHeaders: { token: myToken },
      // 黑名单类别
      roomLevel: [
        { value: 0, label: '公会房间' },
        { value: 1, label: '私人房间' }
      ],
      blackList: [
        { value: 1, label: '1' },
        { value: 2, label: '4' },
        { value: 3, label: '6' },
        { value: 4, label: '3' },
        { value: 5, label: '5' }
      ],
      OfficialTypeOptions: [],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        room_type_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        icon: [{ required: true, message: '请上传icon', trigger: 'blur' }],
        level: [{ required: true, message: '请输入等级', trigger: 'blur' }],
        nature: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      // 受限类型
      limit_typeList: [
        { value: 1, label: '全部' },
        { value: 2, label: 'IP' },
        { value: 3, label: '归属地' }
      ],
      editRulesForm: {
        room_type_name: undefined,
        nature: undefined,
        sort: undefined,
        level: undefined,
        type: 1,
        icon: '',
        higher_id: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [{
        id: 1,
        info: 'test2',
        need_user_level: 222,
        cate: 1,
        state: '1'
      }],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ''
    }
  },
  created() {
    this.getList()
    this.getRoomTypeList()
  },
  methods: {
    getRoomTypeList() {
      roomTypeList().then(res => {
        if (res.code === 200) {
          this.OfficialTypeOptions = res.data.list
        }
      })
    },
    getList() {
      this.listLoading = true
      let urlData = '?'
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`
      }
      urlData += `&type=1`
      listData(urlData).then((response) => {
        // response)
        this.menuList = response.data.list
        this.total = response.data.meta.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id
      this.editRulesForm.sort = row.sort
      this.editRulesForm.room_type_name = row.type_name
      this.editRulesForm.icon = row.icon
      this.editRulesForm.level = row.level
      this.editRulesForm.higher_id = row.higher_id
      this.editRulesForm.nature = row.nature

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          room_type_name: undefined,
          editRulesForm: undefined,
          sort: undefined,
          level: undefined,
          type: 1,
          icon: '',
          higher_id: undefined
        }
      })
    },
    // 获取省份
    getProvince() {

    },
    // 获取城市
    getCity() {

    },
    handleSuccess() {
      this.editRulesForm.icon = this.ossLastImgUrl
      this.$forceUpdate()
    },
    submitForm(formName) {
      if (this.confirmBtn) return
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          // alert('submit!');
          if (this.dialogStatus === 'create') {
            postAddData(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false

              }
            })
          } else {
            putEditData(this.row_id, this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false

              }
            })
          }
        } else {
          // 'error submit!!');
          return false
        }
      })
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      // this.dataIds)
      deleteData(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style scoped>

</style>
